<?php

namespace app\common\service;

use app\admin\service\SmsService;
use think\facade\Cache;
use think\facade\Db;

class MemCacheService
{
    //根据域名判断哪个县市
    public static function checkIsUseMemcache(){
        $res = false;
        preg_match('/(.*\.)?\w+\.\w+$/', $_SERVER['SERVER_NAME'], $matches);
        $domain = '';
        if (!empty($matches[1])) {
            $domain = $matches[1];
        }
        //$domain = 'sb.'; //本地默认，上线删除
        if (in_array($domain,[])){
            $res = true;
        }
        return $res;
    }

    //获取domain前缀，用以拼接memcache前缀
    public static function getDomainPrefix(){
        preg_match('/(.*\.)?\w+\.\w+$/', $_SERVER['SERVER_NAME'], $matches);
        $domain = '';
        if (!empty($matches[1])) {
            $domain = $matches[1];
        }
        //$domain = 'sb.'; //本地默认，上线删除
        $domain = substr($domain,0,-1);
        return $domain;
    }

    //设置全局memcache过期时间
    public static function getExpireTime(){
        $time = 60 * 60 * 10; //10h
        return $time;
    }

    //存储memcache
    public static function setMemCache($key,$value,$expire = 0)
    {
        try {
            $memcache = new \Memcache();
            $memcache->connect('127.0.0.1', 11211);
        } catch (\Exception $e) {
            self::sendNoticeMes();
            return true;
        }

        if ($expire == 0) $expire = MemCacheService::getExpireTime();
        $key = MemCacheService::getDomainPrefix() . '_' . $key;
        $value = serialize($value);
        $memcache->set($key, $value, MEMCACHE_COMPRESSED, $expire);
    }

    //获取memcache
    public static function getMemCache($key){
        try {
            $memcache = new \Memcache();
            $memcache->connect('127.0.0.1',11211);
        } catch (\Exception $e) {
            self::sendNoticeMes();
            return true;
        }

        $key = MemCacheService::getDomainPrefix().'_'.$key;
        $cache = $memcache->get($key);
        if (false != $cache){
            $cache = unserialize($cache);
        }else{
            $cache = NULL;
        }
        return $cache;
    }

    //存储memcache
    public static function setMemCache1($key,$value,$expire = 0){
        if ($expire == 0) $expire = MemCacheService::getExpireTime();
        $key = MemCacheService::getDomainPrefix().'_'.$key;
        $value = serialize($value);
        Cache::store('memcache')->set($key,$value,$expire);
    }

    //获取memcache
    public static function getMemCache1($key){
        try {
            $key = MemCacheService::getDomainPrefix().'_'.$key;
            $cache = Cache::store('memcache')->get($key);
            !is_null($cache) && $cache = unserialize($cache);
            return $cache;
        } catch(\Exception $e){
            self::sendNoticeMes();
        }
    }

    //给管理员发送通知短信 1小时内只触发一次
    private static function sendNoticeMes(){
        $cacheOneHour = cache('oneHour');
        if (is_null($cacheOneHour)){
            cache('oneHour',1,60*60);//一小时缓存，过期后自动变为null
            SmsService::sendNoticeMes();//发送重启缓存通知
        }
    }
}